package com.itheima.mongo.api;

import cn.hutool.core.collection.CollUtil;
import com.itheima.dubbo.api.MovementApi;
import com.itheima.model.mongo.Movement;
import com.itheima.model.mongo.MovementTimeLine;
import com.itheima.model.vo.PageResult;
import com.itheima.mongo.utils.IdWorker;
import com.itheima.mongo.utils.TimeLineService;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@DubboService
public class MovementApiImpl implements MovementApi {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private TimeLineService timeLineService;

    @Override//发布动态
    public void publish(Movement movement) {
        try {
            //保存动态详情
            //设置pid
            movement.setPid(idWorker.getNextId("movement"));
            //设置创建时间
            movement.setCreated(System.currentTimeMillis());
            mongoTemplate.save(movement);
            //查询当前用户好友
            timeLineService.save(movement.getUserId(), movement.getId());
        } catch (Exception e) {
            //忽略事务处理
            e.printStackTrace();
        }
    }


    @Override
    public Movement findone(String id) {
        Movement movement = mongoTemplate.findById(id, Movement.class);
        return movement;
    }

    @Override
    public PageResult all(Integer page, Integer pagesize, Long userId) {
        //构建查询条件
        Criteria criteria = Criteria.where("userId").is(userId);
        Query query = Query.query(criteria).with(Sort.by(Sort.Order.desc("created")))
                .skip((page - 1) * pagesize).limit(pagesize);
        //调用mongo查询集合
        List<Movement> list = mongoTemplate.find(query, Movement.class);
        PageResult pageResult = new PageResult(page, pagesize, 0, list);
        return pageResult;
    }

    //随机生成动态
    @Override
    public List<Movement> getRandom(Integer pagesize) {                             //随机生成的数
        TypedAggregation aggregation = Aggregation.newAggregation(Movement.class, Aggregation.sample(pagesize));
        AggregationResults<Movement> movements = mongoTemplate.aggregate(aggregation, Movement.class);
        return movements.getMappedResults();
    }

    //生成数据中的动态
    @Override
    public List<Movement> findPids(List<Long> pids) {
        //创建查询条件
        Criteria criteria = Criteria.where("pId").in(pids);
        Query query = Query.query(criteria);
        List<Movement> movementList = mongoTemplate.find(query, Movement.class);
        return movementList;
    }

    @Override
    public List<Movement> FriendMovement(Long userId, Integer page, Integer pagesize) {
        //创建查询条件进行分页
        Criteria criteria = Criteria.where("friendId").is(userId);
        Query query = Query.query(criteria).skip((page - 1) * pagesize).limit(pagesize)
                .with(Sort.by(Sort.Order.desc("created")));
        //查询时间线表的动态
        List<MovementTimeLine> lines = mongoTemplate.find(query, MovementTimeLine.class);
        //获取动态id
        List<ObjectId> ids = CollUtil.getFieldValues(lines, "movementId", ObjectId.class);
        //创建查询条件
        Query movementQuery=Query.query(Criteria.where("id").in(ids));
        //返回数据
        List<Movement> list = mongoTemplate.find(movementQuery, Movement.class);
        return list;
    }



}
